package com.glSto.config;

import com.glSto.req.BomRequest;
import com.glSto.req.PurchaseRequest;
import com.glSto.req.GoodsIssueRequest;
import com.glSto.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;

@Component
public class ScheduledTaskManager {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTaskManager.class);

    @Autowired
    private MdmService mdmService;

    @Autowired
    private BomService bomService;

    @Value("${mdm.tBasicAuth.username}")
    private String userName;

    @Value("${mdm.tBasicAuth.password}")
    private String passWord;

    /**
     * 定时获取主数据 - 每天凌晨0点执行
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void scheduledGetMainData() {
        try {
            logger.info("开始执行主数据定时任务");
            String msg = mdmService.getMainData(userName, passWord);
            logger.info("主数据定时任务执行成功: {}", msg);
        } catch (Exception e) {
            logger.error("主数据定时任务执行异常", e);
        }
    }

    /**
     * 定时向BOM发送请求 - 每天凌晨1点执行
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void scheduledBomRequest() {
        try {
            logger.info("开始执行BOM请求定时任务");
            BomRequest request = new BomRequest();
            BomRequest.IsParam isParam = new BomRequest.IsParam();
            isParam.setEQUNR("?");
            isParam.setTIDNR("?");
            request.setIS_PARAM(isParam);
            int result = bomService.getBOMData(request);
            if (result == 1) {
                logger.info("BOM请求定时任务执行成功，等待对方平台回调");
            } else {
                logger.warn("BOM请求定时任务执行失败");
            }
        } catch (Exception e) {
            logger.error("BOM请求定时任务执行异常", e);
        }
    }

    /**
     * 定时获取采购数据 - 每天凌晨2点执行
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void scheduledGetGoodsPurchaseData() {
        try {
            logger.info("开始执行采购数据定时任务");
            PurchaseRequest request = new PurchaseRequest();
            PurchaseRequest.ImMessage imMessage = new PurchaseRequest.ImMessage();
            imMessage.setIM_GUID("1");
            imMessage.setSEND_TIME("1");
            imMessage.setSENDER("1");
            imMessage.setSEND_CFID("1");
            imMessage.setRECEIVER("1");
            imMessage.setRECEIVE_IFID("1");
            imMessage.setRECEIVE_METHOD("1");
            imMessage.setSEND_SYSTEM_ID("1");
            imMessage.setPROXY_ID("1");
            imMessage.setMANDT("1");
            imMessage.setSEND_OPERATOR("1");
            imMessage.setSPRAS("1");

            request.setIM_MESSAGE(imMessage);
            request.setIT_DATA(Collections.emptyList()); // 设置空数组

            mdmService.getPurchaseData(request);
            logger.info("采购数据定时任务执行成功");
        } catch (Exception e) {
            logger.error("采购数据定时任务执行异常", e);
        }
    }

    /**
     * 定时获取物资出库数据 - 每天凌晨3点执行
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void scheduledGetGoodsIssueData() {
        try {
            logger.info("开始执行物资出库数据定时任务");
            GoodsIssueRequest request = new GoodsIssueRequest();
            GoodsIssueRequest.ImMessage imMessage = new GoodsIssueRequest.ImMessage();
            imMessage.setIM_GUID("1");
            imMessage.setSEND_TIME("1");
            imMessage.setSENDER("1");
            imMessage.setSEND_CFID("1");
            imMessage.setRECEIVER("1");
            imMessage.setRECEIVE_IFID("1");
            imMessage.setRECEIVE_METHOD("1");
            imMessage.setSEND_SYSTEM_ID("1");
            imMessage.setPROXY_ID("1");
            imMessage.setMANDT("1");
            imMessage.setSEND_OPERATOR("1");
            imMessage.setSPRAS("1");

            request.setIM_MESSAGE(imMessage);
            request.setIT_DATA(Collections.emptyList()); // 设置空数组

            mdmService.getGoodsIssueData(request);
            logger.info("物资出库数据定时任务执行成功");
        } catch (Exception e) {
            logger.error("物资出库数据定时任务执行异常", e);
        }
    }
}
